import { queryPoints, queryMemberCouponDiscount } from "@src/api/order";
import { discountMd5 } from "@src/common/discountUtils";
import router from "@src/router";
import { storeBindingsBehavior } from "mobx-miniprogram-bindings";
import mall from "@src/store/mall";
import base from "@src/store/base";
Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: mall,
    actions: ['setStoreId']
  }, {
    store: base,
    fields: ['themeColor', 'themeTitleColor']
  }],
  properties: {
    items: {
      type: Object,
      value: []
    },
    snCode:{
      type:String,
      value:""
    },
    invalidItems: {
      type: Object,
      value: []
    },

    store: {
      type: Object,
    },
    delivery: {
      type: Object,
    },
    orderPayInfo: {
      type: Object,
    },
    deliveryType: {
      type: String
    },
    freightFee: {
      type: Number
    },
    arrivalTime: {
      type: String
    },
    prepareTips: {
      type: String
    },
    remark: {
      type: String,
      value: ""
    },
    pointsDiscount: {
      type: Object,
      value: null
    },
    discountInfo: {
      type: Object,
      value: null
    },
    presell: {
      type: Boolean,
      value: false,
    },
    goodsPresell: {
      type: Object
    }
  },
  data: {
    discountSize: 0,
    userPoints: null,
    preInfo:null,
    activityShow:false,
    goodsItmes:null
  },
  observers: {
    'orderPayInfo,store': function (orderPayInfo, store) {
      if (orderPayInfo && store && orderPayInfo.supplyDiscount) {
        this.fetchDiscount();
        this.fetchPoints();
      }
    },
  },
  methods: {
    fetchDiscount() {
      let tradeFee = this.data.orderPayInfo.totalFee - this.data.orderPayInfo.unDiscountableFee;
      // let tradeFee = this.data.orderPayInfo.tradeFee;
      let goodsItems = this.data.items.map((item,index,arr)=>{
        const  goodsCode = this.data.snCode||item.barCode;
        // return {goodsCode:goodsCode,num:item.num,price:item.price,settings:item.settings}
        return {goodsCode:goodsCode,num:item.num,price:item.discountPrice,settings:item.settings}
      })
      // this.setData({goodsItems:goodsItems})
      let data = {bizCode:9013, goodsItems:goodsItems, orderType: 5, shopId: this.data.store.shopId, tradeFee: tradeFee }
      queryMemberCouponDiscount(data).then(res => {
        if (res.code === 0) {
          let couponList = res.data;

          //选取默认最优的优惠
          let discountList = res.data.sort((item1,item2)=>{
            return item2.discountFee - item1.discountFee
          })
          const chooseInfo = discountList[0];
          res.data.forEach((item,index)=>{
            if(item.discountFee === discountList[0].discountFee){
              chooseInfo.selectContId =  index
            }
          })
          this.triggerEvent("discount", chooseInfo)
          this.setData({
            chooseInfo:discountList[0],
            discountInfo:discountList[0],
            discountSize: couponList.length ,
            preInfo:discountList,
          })

        }
      })
    },
    fetchPoints() {
      let tradeFee = this.data.orderPayInfo.totalFee - this.data.orderPayInfo.unDiscountableFee;
      let goodsItems = this.data.items.map((item,index,arr)=>{
        return {goodsCode:item.goodsSerial,num:item.num,price:item.price,settings:item.settings}
      })
      let data = { orderType: 5, shopId: this.data.store.shopId, tradeFee: tradeFee,  goodsItems:goodsItems }
      console.log(this.data)
      queryPoints(data).then(res => {
        if (res.code === 0) {
          this.setData({ userPoints: res.data })
        }
      })
    },
    handleInputRemark() {
      let params = { title: "备注信息", text: this.data.remark };
      this.selectComponent("#inputDialog").open(params).then(remark => {
        this.triggerEvent('remark', remark)
      });
    },
    handleSelectPoints(e) {
      this.triggerEvent("points", e.detail)
    },
    handleToStores() {
      let deliveryType = this.data.deliveryType
      router.push(
        {
          'name': 'mall/service-stores',
          params: { deliveryType: deliveryType },
          events: {
            'changeStore': (data) => {
              this.setStoreId(data.store.storeId)
              this.triggerEvent("store", data)
            }
          }
        });
    },
    handleToSelectTime() {
      this.triggerEvent("arrivalTime")
    },
    handleToPoints() {
      if (this.data.userPoints && this.data.userPoints.points > 0) {
        this.selectComponent("#pointsDialog").open()
      }
    },
    handleToDiscount() {
      if (this.data.discountSize <= 0) {
        return;
      }
      let discountInfo = this.data.discountInfo;
      let selectItem = discountMd5(discountInfo)
      this.setData({activityShow:true});
      // router.push({
      //   name: 'selectDiscount',
      //   params: {
      //     shopId: this.data.store.shopId,
      //     totalFee: this.data.orderPayInfo.totalFee - this.data.orderPayInfo.unDiscountableFee,
      //     orderType: 5,
      //     type: 'memberCoupon',
      //     selectItem: selectItem
      //   },
      //   events: {
      //     'change': (newDiscountInfo) => {
      //       console.info(newDiscountInfo)
      //
      //     }
      //   }
      // })
    },
    LevelCommentBtn(e){
      console.log(e,"这是传过来的值");
      this.setData({discountInfo:e.detail})
      this.triggerEvent("discount", e.detail)
    }
  }
});
